package com.ken.springcloud.controller;

import com.ken.springcloud.pojo.Dept;
import com.ken.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <ul>
 * <li>Title: DeptController</li>
 * <li>Description: TODO </li>
 * </ul>
 *
 * @author swc
 * @date 2021-08-25 16:12
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixqueryAll")
    public Dept queryAll(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept==null){
            throw  new RuntimeException("id is null and this database is not found");
        }
        return dept;
    }


    //这里有个比较坑的地方 就是回调方法 需要跟当前被调用的方法 参数一致
    public Dept hystrixqueryAll(@PathVariable("id") Long id){
        return  new Dept().setDeptNo(id).setDeptName("没有找到").setDbSource("this database is not found");
    }


}
